package com.sdjzu.wanfuying.androiddesign.mvvm.bindingadapter;

import android.annotation.SuppressLint;
import android.view.View;
import android.widget.Button;

import com.jakewharton.rxbinding2.view.RxView;

import java.util.concurrent.TimeUnit;

import androidx.databinding.BindingAdapter;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;

public class DebounceAdapter {
    @SuppressLint("CheckResult")
    @BindingAdapter(value = {"debounce", "click"}, requireAll = false)
    public static void debounce(Button view, Integer timeout, View.OnClickListener listener) {
        if (null == timeout || null == listener)
            return;
        RxView.clicks(view)
                .throttleFirst(timeout, TimeUnit.MILLISECONDS, AndroidSchedulers.mainThread())
                .subscribe(o -> listener.onClick(view));
    }
}
